const maxAreaOfIsland = (grid) => {
  const x = grid.length,
    y = grid[0].length;
  let max = 0;
  for (let i = 0; i < x; i++) {
    for (let j = 0; j < y; j++) {
      if (grid[i][j] == 1) {
        max = Math.max(max, dfs(grid, i, j, x, y));
      }
    }
  }
  return max;
};

function dfs(grid, i, j, x, y) {
  if (i < 0 || i >= x || j < 0 || j >= y || grid[i][j] == 0) return 0;
  let count = 1;
  grid[i][j] = 0;
  count += dfs(grid, i - 1, j, x, y);
  count += dfs(grid, i + 1, j, x, y);
  count += dfs(grid, i, j - 1, x, y);
  count += dfs(grid, i, j + 1, x, y);
  return count;
}
